using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Utilities;

namespace CustomAdministration.Features.AdminPages_Web
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid("d569ac33-d367-41ff-a090-bfdb2968cb63")]
    public class AdminPages_WebEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;

            if (web != null)
            {
                string homepage = "adminpage.aspx";
                string securitypage = "securitypage.aspx";

                // Set the home page of the web
                SPFolder rootFolder = web.RootFolder;
                rootFolder.WelcomePage = homepage;
                rootFolder.Update();

                // Add quick launch links
                SPNavigationNode adminHeading = new SPNavigationNode("Contoso Administration", SPUrlUtility.CombineUrl(web.ServerRelativeUrl, homepage));
                web.Navigation.QuickLaunch.AddAsFirst(adminHeading);

                SPNavigationNode security = new SPNavigationNode("Security", SPUrlUtility.CombineUrl(web.ServerRelativeUrl, securitypage));
                adminHeading.Children.AddAsFirst(security);
            }
        }
    }
}
